﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Mvc;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;

namespace Portal.Presentation
{
	public class DependencyInjectionContainer
	{
		public static void Resolve()
		{
			var builder = new ContainerBuilder();

			var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>().Where(x => x.FullName.StartsWith("Portal")).ToList();
			var types  = assemblies.SelectMany(x => x.GetTypes()).ToList();
			foreach (var type in types)
			{
				Console.WriteLine(type.ToString());

				if (typeof(IModule).IsAssignableFrom(type))
				{
					if (type.CanBeInstantiated())
					{
						builder.RegisterModule((IModule)Activator.CreateInstance(type));
					}
				}
			}

			var container = builder.Build();
			DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

		}


	}
}